import Vue from 'vue'
import { createRouter, createWebHashHistory } from 'vue-router'
// 导入路由
import Movie from '@/views/Movie'
import Cinema from '@/views/Cinema'
import My from '@/views/My'
import News from '@/views/News'
import nowplaying from '@/views/Movie/nowplaying'
import moviedetail from '@/views/Movie/moviedetail'
import Detail from '@/views/Movie/details.vue'
import City from '@/views/Movie/city.vue'
import Login from '@/views/My/login.vue'
import Register from '@/views/My/register.vue'
// import search from '@/views/Cinema/search.vue'
import xiangxi from '@/views/Cinema/xiangxi.vue'
import chinemaDetail from '@/views/Cinema/chinemaDetail.vue'

import store from '@/store/index'
import { createApp } from 'vue';
const app = createApp()

const routes = [
  {
    path: '/',
    redirect: '/movie/nowplaying'
  },
  {
    path: '/movie',
    component: Movie,
    children: [
      {
        path: 'nowplaying',
        component: nowplaying
      },
      {
        path: 'moviedetail',
        component: moviedetail
      },
    ]
  },
  {
    path: '/my',
    component: My
  },
  {
    path: '/cinema',
    component: Cinema
  },
  {
    path: '/news',
    component: News
  },
  {
    path: '/detail',
    component: Detail
  },
  {
    path: '/city',
    component: City
  },
  ,
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/chinemaDetail',
    component: chinemaDetail
  },
  {
    path: '/xiangxi',
    component: xiangxi
  }
]

const router = new createRouter({
  mode:'hash',
  history: createWebHashHistory(),
  routes
})

// 让路由可以跳转自身页面
// const originalPush = createApp.prototype.push
// createApp.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch((err) => err)
// }
// 路由对象有两个钩子函数 - beforeEach  afterEach
// beforeEach是路由即将跳转的时候会执行的一个钩子函数
router.beforeEach((to, from, next) => {
  // 把地址组个数组
  let arr = ['/movie', '/cinema', '/news', '/my']
  let index = arr.indexOf(to.path)
  store.commit('changeActive', index)
  next()
})

export default router
